<template>
  <BasicTable @register="registerTable">
    <template #toolbar>
      <Popconfirm title="确定要删除吗"
                  ok-text="确认"
                  cancel-text="取消"
                  @confirm="handleBatchDeletUser">
        <a-button type="primary">批量删除</a-button>
      </Popconfirm>
    </template>
    <template #action="{ record, column }">
      <TableAction :actions="createActions(record, column)" />
    </template>
  </BasicTable>

  <Modal @register="registerModal"
         @success="handleSuccess" />
  <!-- 驳回 -->
  <ApplicationModal @register="registerModal1"
                    @successModel="handleSuccess" />
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import {
  ActionItem,
  BasicColumn,
  BasicTable,
  EditRecordRow,
  FormSchema,
  useTable,
  TableAction,
} from '/@/components/Table';
import { ListApia, ChangeStatusApi, TuckApi, DeleteApi } from '/@/api/user/internationalStudents';
import { genderTypes, studentStatus } from '/@/assets/dic';
import formatDic from '/@/utils/formatDic';
import { UserItem } from '/@/api/user/userModel';
import Modal from './modal.vue';
import { useModal } from '/@/components/Modal';
import { Popconfirm } from 'ant-design-vue';
import { areaSearch } from '/@/utils/areaSearch';
import ApplicationModal from './applicationModal.vue';
import { GetcitylistApi } from '/@/api/activityAdmin/index';
import { AreaList } from './data';
const columns: BasicColumn[] = [
  {
    title: '昵称',
    dataIndex: 'nickName',
  },
  {
    title: '姓名',
    dataIndex: 'name',
  },
  // {
  //   title: '性别',
  //   dataIndex: 'gender',
  //   format: formatDic(genderTypes),
  // },
  {
    title: '学校',
    dataIndex: 'university',
  },
  {
    title: '邮箱',
    dataIndex: 'email',
  },
  {
    title: '手机号',
    dataIndex: 'phoneNumber',
  },
  // {
  //   title: '地区',
  //   dataIndex: 'a',
  // },
  {
    title: '详细地址',
    dataIndex: 'street',
  },
  {
    title: '区域',
    dataIndex: 'bigareaForm',
  },
  {
    title: '提交认证时间',
    dataIndex: 'createTime',
  },
  {
    title: '状态',
    dataIndex: 'states',
    format: formatDic(studentStatus),
  },
];

const colProps = {
  span: 6,
};
const schemas: FormSchema[] = [
  {
    label: '姓名：',
    field: 'name',
    component: 'Input',
    colProps,
  },
  {
    label: '手机号：',
    field: 'phoneNumber',
    component: 'Input',
    colProps,
  },
  {
    label: '邮箱：',
    field: 'email',
    component: 'Input',
    colProps,
  },
  {
    label: '状态：',
    field: 'states',
    component: 'Select',
    componentProps: {
      options: studentStatus,
    },
    colProps,
  },
  {
    label: '区域：',
    field: 'city',
    component: 'AreaCascader',
    colProps,
  },
  {
    field: 'createTime',
    label: '时间：',
    component: 'RangePicker',
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
    },
    colProps,
  },
];

export default defineComponent({
  name: 'AverageUser',
  components: {
    BasicTable,
    TableAction,
    Modal,
    ApplicationModal,
    Popconfirm,
  },
  setup() {
    const citylist = ref<AreaList[]>([]);
    // const _schemas = areaSearch(schemas, schemas.length - 1, colProps);
    // const selectedRowKeys = ref<string[]>([]);
    const [registerModal, { openModal }] = useModal();
    const [registerModal1, { openModal: openModal1 }] = useModal();
    const [registerTable, { reload, getSelectRows }] = useTable({
      api: ListApia,
      useSearchForm: true,
      rowSelection: {
        type: 'checkbox',
      },
      columns,
      formConfig: {
        schemas: schemas,
        labelWidth: 100,
      },
      beforeFetch(params) {
        if (Array.isArray(params.city)) {
          params.area = params.city[1];
          params.city = params.city[0];
        }
        if (Array.isArray(params.createTime)) {
          params.createTime = params.createTime.join('~');
        }
      },
      afterFetch(record) {
        record.forEach((element) => {
          const cityItem = citylist.value.filter((item) => {
            return item.id == element.city;
          });
          
          if (cityItem.length > 0) {
            element.bigareaForm = `${cityItem[0].city || ''}-${element.area || ''}`;
          }
        });
      },
      actionColumn: {
        width: 200,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
      },
    });

    function handleSuccess() {
      reload();
    }
    // 改变用户的使用状态
    function handleChangeStatus(record: UserItem) {
      record.isUse = record.isUse === 0 ? 1 : 0;
      ChangeStatusApi(record).then((_) => {
        reload();
      });
    }

    //发卡
    function tuckcomb(record: UserItem) {
      // console.log(666);
      const data = {
        id: record.id,
      };
      TuckApi(data).then((_) => {
        reload();
      });
    }
    //驳回 reject
    function rejectStatus(record: UserItem) {
      record.isUse = 3;
      // EditApi(record).then((_) => {
      //   reload();
      // });
    }
    // 删除用户
    function handleDeletUser(ids: number | number[]) {
      console.log(ids);
      DeleteApi(ids).then((_) => {
        reload();
      });
    }
    //打开驳回
    function handleCreate(record) {
      openModal(true, {
        record,
        isUpdate: false,
      });
    }
    // 拿到需要批量删除的id
    function handleBatchDeletUser() {
      const selectRows = getSelectRows();
      const ids = selectRows.map((item) => item.id);
      handleDeletUser(ids);
    }
    function Getcitylist() {
      GetcitylistApi().then((res) => {
        if (res.code == 200) {
          citylist.value = res.data;
        }
      });
    }
    Getcitylist();
    function createActions(record: EditRecordRow, _: BasicColumn): ActionItem[] {
      if (record.states == 1) {
        return [
          {
            label: '查看',
            color: 'success',
            onClick() {
              openModal(true, {
                record,
                citylist,
                isUpdate: true,
              });
            },
          },
          {
            // label: record.isUse === 0 || record.isUse === 3 ? '发卡' : '已发卡',
            label: '发卡',
            onClick: tuckcomb.bind(null, record as UserItem),
          },
          {
            label: '驳回',
            color: 'error',
            onClick() {
              openModal1(true, {
                record,
              });
            },
          },
          {
            label: '删除',
            color: 'error',
            popConfirm: {
              title: '确定要删除吗！',
              confirm() {
                handleDeletUser(record.id);
              },
            },
          },
        ];
      } else {
        return [
          {
            label: '查看',
            color: 'success',
            onClick() {
              openModal(true, {
                record,
                isUpdate: true,
              });
            },
          },
          {
            label: '删除',
            color: 'error',
            popConfirm: {
              title: '确定要删除吗！',
              confirm() {
                handleDeletUser(record.id);
              },
            },
          },
        ];
      }
    }
    return {
      registerTable,
      registerModal1,
      openModal1,
      handleCreate, //打开驳回
      createActions,
      handleBatchDeletUser,
      registerModal,
      handleSuccess,
      tuckcomb,
    };
  },
});
</script>
<style lang="less" scoped></style>
